package com.iseastar.guojiang.loc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import droid.frame.activity.HandlerMgr;
import droid.frame.utils.android.Log;
import droid.frame.utils.lang.Str;

/**
 * Created by joker on 2017/12/20.
 * 用于火箭侠刚进来的时候进行定位
 */
public class CourierLocationService1 extends Service implements BDLocationListener {

    // 定位相关
    private LocationClient mLocClient;
    //
    private int msgId = -1;

    @Override
    public void onCreate() {
        super.onCreate();
        initBaiDu();
        //
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            msgId = intent.getIntExtra("msgId", -1);
        }
        Log.d("33333", "onStartCommand()");
        // 刷新定位
        if (mLocClient != null && mLocClient.isStarted()) {
            mLocClient.requestLocation();
        } else {
            initBaiDu();
        }
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 初始化百度地图
     */
    private void initBaiDu() {
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(this);
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);// 设置以后,请求结果 BDLocation#getCity 就不为null了
        //        option.setOpenGps(true);// 打开gps
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(20000);// 定位频率
        //可选，定位SDK内部是一个service，并放到了独立进程。
        //设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)
        option.setIgnoreKillProcess(true);
        option.setPriority(LocationClientOption.NetWorkFirst);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }


    /**
     * 定位SDK监听函数
     */
    @Override
    public void onReceiveLocation(BDLocation location) {
        int errorCode = location.getLocType();
        Log.d("33333", "错误码：" + errorCode);
        if (location == null || Str.isEmpty(location.getCity())) {
            // 刷新定位
            if (mLocClient != null) {
                mLocClient.unRegisterLocationListener(this);
                mLocClient.stop();
                mLocClient = null;
                initBaiDu();
            } else {
                initBaiDu();
            }
        } else {
            Message msg = Message.obtain();
            msg.what = msgId;
            msg.obj = location;
            HandlerMgr.sendMessage(msg, 0);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 退出时销毁定位
        if (mLocClient != null) {
            mLocClient.stop();
            mLocClient = null;
        }
        //
    }

}
